package com.hup.minicontroller.common.framework;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.arch.core.util.Function;
import androidx.core.util.Consumer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import lombok.Getter;

@SuppressWarnings("UnusedReturnValue")
public class BaseApplication extends Application {

    @Getter
    private static BaseApplication instance;

    private HashMap<Class<?>, Object> mapBean;

    @Override
    public void onCreate() {
        super.onCreate();
        mapBean = new HashMap<>();
        instance = this;
    }

    @Override
    public void onTerminate() {
        mapBean.clear();
        mapBean = null;
        instance = null;
        super.onTerminate();
    }

    protected void printStartLogo(String TAG) {
        Log.i(TAG, "====================");
        Log.i(TAG, "=====  " + getClass().getSimpleName() + "正在启动");
        Log.i(TAG, "=====  " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
        Log.i(TAG, "====================");
    }

    protected void printStartedLogo(String TAG) {
        Log.i(TAG, "====================");
        Log.i(TAG, "=====  " + getClass().getSimpleName() + "启动完成");
        Log.i(TAG, "=====  " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
        Log.i(TAG, "====================");
    }

    public static Context getContext() {
        return instance;
    }

    public static <T> T putBean(T bean) {
        instance.mapBean.put(bean.getClass(), bean);
        return bean;
    }

    public static <T> T putBean(Class<T> clz, T bean) {
        instance.mapBean.put(clz, bean);
        return bean;
    }

    public static void removeBean(Class<?> clz) {
        instance.mapBean.remove(clz);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clz) {
        T res = (T) instance.mapBean.get(clz);
        if (res == null) throw new IllegalArgumentException("找不到bean:" + clz.getName());
        return res;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBeanNullable(Class<T> clz) {
        return (T) instance.mapBean.get(clz);
    }

    /**
     * @return hadExe
     */
    @SuppressWarnings("unchecked")
    public static <T> boolean executeIfExist(Class<T> clz, Consumer<T> consumer) {
        T bean = (T) instance.mapBean.get(clz);
        if (bean != null) {
            consumer.accept(bean);
            return true;
        } else {
            return false;
        }
    }

    @SuppressWarnings("unchecked")
    public static <T, I, O> O executeIfExist(Class<T> clz, I param, Function<I, O> function) {
        T bean = (T) instance.mapBean.get(clz);
        if (bean == null) return null;
        return function.apply(param);
    }

    /**
     * @return hadExe
     */
    @SuppressWarnings("unchecked")
    public static <T> boolean executeForeach(Class<? extends List<T>> clz, Consumer<T> consumer) {
        List<T> list = (List<T>) instance.mapBean.get(clz);
        boolean hadExe = false;
        if (list != null) {
            for (T item : list) {
                consumer.accept(item);
                hadExe = true;
            }
        }
        return hadExe;
    }

}
